package com.dhcc.sds.busi.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import cn.hutool.core.date.DateTime;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.dhcc.core.framework.util.CommonUtil;
import com.dhcc.sds.base.entity.SdsQcentityEntity;
import com.dhcc.sds.base.entity.SdsQcentityRuleEntity;
import com.dhcc.sds.base.service.ISdsQcentityRuleService;
import com.dhcc.sds.busi.vo.AnalysisSdsPortalVo;
import com.dhcc.sds.busi.vo.SdsOPQcincaseVo;
import io.lettuce.core.protocol.Command;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.plugins.Page;
import com.dhcc.core.framework.base.service.impl.BaseServiceImpl;
import com.dhcc.sds.busi.dao.SdsQcincaseOperlogMapper;
import com.dhcc.sds.busi.service.ISdsQcincaseOperlogService;
import com.dhcc.sds.busi.entity.SdsQcincaseOperlogEntity;
import org.springframework.util.StringUtils;

/**
 * 病例操作日志表数据服务层接口
 * 
 * @ClassName: SdsQcincaseOperlogServiceImpl
 * @Description: TODO
 * @author sunyingjie
 * @date 2021-03-22 10:09:14
 */
@Service("sdsQcincaseOperlogService")
public class SdsQcincaseOperlogServiceImpl extends BaseServiceImpl<SdsQcincaseOperlogMapper, SdsQcincaseOperlogEntity> implements ISdsQcincaseOperlogService {

	@Autowired
	private ISdsQcentityRuleService sdsQcentityRuleService;

	@Override
	public List<SdsQcincaseOperlogEntity> page(Page<SdsQcincaseOperlogEntity> page, Map<String, Object> map) {
		List<SdsQcincaseOperlogEntity> list = this.baseMapper.page(page, map);
		for (int i = 0; i < list.size(); i++) {
			if (CommonUtil.isNotEmpty(list.get(i).getOperReason())) {
				String str = "";
				for (String sid : list.get(i).getOperReason().split(",")) {
					SdsQcentityRuleEntity rule = sdsQcentityRuleService.selectById(sid);
					if (rule!=null) {
						str += rule.getRuleDesc() + ",";
					}
				}
				list.get(i).setOperReasonDesc(str);
			}
		}
		return list;
	}
	@Override
	public List<SdsQcincaseOperlogEntity> list(Map<String, Object> map) {
		return this.baseMapper.list(map);
	}

	@Override
	public List<Integer> selectPortalCount(Map<String, Object> params) {
		return this.baseMapper.selectPortalCount(params);
	}

	@Override
	public List<AnalysisSdsPortalVo> selectMainChartData(DateTime nowDate, DateTime lastYearDate, Map<String, Object> params) {
		return this.baseMapper.selectMainChartData(nowDate, lastYearDate, params);
	}

	@Override
	public List<AnalysisSdsPortalVo> selectQCSumChartData(Map<String, Object> params) {
		return this.baseMapper.selectQCSumChartData(params);
	}

	@Override
	public List<AnalysisSdsPortalVo> selectLocSumChartData(Map<String, Object> params) {
		return this.baseMapper.selectLocSumChartData(params);
	}

	@Override
	public List<AnalysisSdsPortalVo> selectDocSumChartData(Map<String, Object> params) {
		return this.baseMapper.selectDocSumChartData(params);
	}

	@Override
	public List<SdsQcincaseOperlogEntity> selectUseTime(Long caseId, List<String> status) {
		return this.baseMapper.selectUseTime(caseId, status);
	}

	@Override
	public List<AnalysisSdsPortalVo> selectPortalCountDetails(String idName, List<String> deptIds) {
		return this.baseMapper.selectPortalCountDetails(idName, deptIds);
	}

	@Override
	public List<AnalysisSdsPortalVo> selectByCondition(Map<String, Object> params) {
		return this.baseMapper.selectByCondition(params);
	}

	@Override
	public List<AnalysisSdsPortalVo> selectPortalCountDetailsList(String idName, List<String> deptIds) {
		return this.baseMapper.selectPortalCountDetailsList(idName, deptIds);
	}

	@Override
	public List<AnalysisSdsPortalVo> selectByConditionList(Map<String, Object> params) {
		return this.baseMapper.selectByConditionList(params);
	}

	@Override
	public List<AnalysisSdsPortalVo> selectQCSumChartDataList(Map<String, Object> params) {
		return this.baseMapper.selectQCSumChartDataList(params);
	}

	@Override
	public List<AnalysisSdsPortalVo> selectLocSumChartDataList(Map<String, Object> params) {
		return this.baseMapper.selectLocSumChartDataList(params);
	}

	@Override
	public List<AnalysisSdsPortalVo> selectDocSumChartDataList(Map<String, Object> params) {
		return this.baseMapper.selectDocSumChartDataList(params);
	}

	@Override
	public List<Long> selectIncaseIdsByCondition(Map<String, Object> params) {
		return this.baseMapper.selectIncaseIdsByCondition(params);
	}

	@Override
	public String getInGroupOrOutOperReason(Long inCaseId, String state) {
		String res = "";
		List<SdsQcincaseOperlogEntity> sdsQcincaseOperlogEntities = this.selectList(new EntityWrapper<SdsQcincaseOperlogEntity>()
				.eq("in_case_id", inCaseId)
				.eq("oper_state", state)
				.orderBy("oper_date", false));
		if (!sdsQcincaseOperlogEntities.isEmpty()){
			String operReason = sdsQcincaseOperlogEntities.get(0).getOperReason();
			String operResume = sdsQcincaseOperlogEntities.get(0).getOperResume();
			if (!StringUtils.isEmpty(operReason)){
				String[] split = operReason.split(",");
				for (String s : split){
					SdsQcentityRuleEntity sdsQcentityRuleEntity = sdsQcentityRuleService.selectById(Long.parseLong(s));
					if (!StringUtils.isEmpty(sdsQcentityRuleEntity)){
						res = res + "," + sdsQcentityRuleEntity.getRuleDesc();
					}
				}
				res = res.substring(1);
				if (!StringUtils.isEmpty(operResume)){
					res = res + "," + operResume;
				}
			}
		}
		return res;
	}

	@Override
	public List<SdsOPQcincaseVo> selectOPIncaseByCondition(Map<String, Object> params) {
		return this.baseMapper.selectOPIncaseByCondition(params);
	}

	@Override
	public SdsQcincaseOperlogEntity selectLastOne(Long mrId, String state) {
		return this.baseMapper.selectLastOne(mrId, state);
	}


}